package com.meida.module.bus.provider.service.impl;

import com.meida.common.base.utils.FlymeUtils;
import com.meida.module.bus.client.entity.BusScholartyrant;
import com.meida.module.bus.client.enums.ScholarTyrantCheckStateEnum;
import com.meida.module.bus.provider.mapper.BusScholartyrantMapper;
import com.meida.module.bus.provider.service.BusScholartyrantService;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import org.springframework.transaction.annotation.Propagation;

/**
 * 学霸认证信息表接口实现类
 *
 * @author flyme
 * @date 2021-08-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusScholartyrantServiceImpl extends BaseServiceImpl<BusScholartyrantMapper, BusScholartyrant> implements BusScholartyrantService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusScholartyrant scholartyrant, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusScholartyrant> cq, BusScholartyrant scholartyrant, EntityMap requestMap) {
        cq.orderByDesc("scholartyrant.createTime");
        return ResultBody.ok();
    }

    @Override
    public EntityMap getScholartyrantInfo(Long userId) {
        CriteriaQuery cq = new CriteriaQuery(BusScholartyrant.class);
        cq.select(BusScholartyrant.class, "scholartyrantId", "subjectInfo", "tags");
        cq.select(AppUser.class, "avatar", "nickName", "userNo");
        cq.eq(BusScholartyrant.class, "scholartyrantId", userId);
        cq.createJoin(AppUser.class).setMainField("scholartyrantId").setJoinField("userId");
        return findOne(cq);
    }

    @Override
    public Boolean isScholarTyrant(Long userId) {
        CriteriaQuery cq = new CriteriaQuery(BusScholartyrant.class);
        cq.eq("scholartyrantId", userId);
        cq.eq("state", ScholarTyrantCheckStateEnum.checkPass.getValue());
        Long count = count(cq);
        return count == 1 ? true : false;
    }

    @Override
    public Integer getScholarTyrantState(Long userId) {
        CriteriaQuery cq = new CriteriaQuery(BusScholartyrant.class);
        cq.addSelect("state");
        cq.eq("scholartyrantId", userId);
        EntityMap entityMap = findOne(cq);
        if (FlymeUtils.isNotEmpty(entityMap)) {
            return entityMap.getInt("state");
        }
        return null;
    }

    @Override
    public Long countNumByUserId(Long userId) {
        CriteriaQuery cq = new CriteriaQuery(BusScholartyrant.class);
        cq.eq("scholartyrantId", userId);
        return count(cq);
    }
}
